<template>
	<view class="content">
		
		<!-- 上部 -->
		<view class="search-box search">
			<!-- 跳转前一页 -->
			<view style="width: 50upx;margin-right: 3upx;" @click="goPrePage" >
				<image src="../../static/icon/left.png" style="width: 35upx; height: 50upx; margin:auto;" class="grayscale"></image>
			</view>
			<view class="input-box">
				<!-- 输入框 -->
				<input type="text" adjust-position="true" v-model="keyword" :placeholder="defaultKeyword" 
				placeholder-class="search-text" @confirm="doSearch" confirm-type="search"
				@input="bindValue"/>
				<!-- 清空按钮 -->
				<image src="../../static/search/close.png" @tap="clears"
				v-if="shows"></image>
			</view>
			<!-- 搜索按钮 -->
			<view  class="search-btn" @tap="doSearch" >搜索</view>
		</view>
		
		<!-- 下部 -->
		<view class="search-keyword">			
			<scroll-view scroll-y class="keyword-box">
				
				<!-- 有历史记录才显示 -->
				<view class="keyword-block" v-if="oldKeywordList.length>0">
						<view class="keyword-list-header">
							<view>历史搜索</view>
							<!-- 清空记录按钮 -->
							<view>
								<image src="../../static/search/delete.png" @tap="oldDelete"></image>
							</view>
						</view>
						<!-- 历史关键字 -->
						<view class="keyword">
							<view v-for="(item, index) in oldKeywordList" :key="index" @tap="ondoSearch(item)">{{item}}</view>
						</view>
				</view>
				
				<!-- 无历史记录-->
				<view v-else class="nohistory">
					<text style="font-size: 150%;" class="cu-tag  round text-grey" >暂无搜索记录</text>
				</view>
				
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				defaultKeyword: "", 
				keyword: "",	
				oldKeywordList: [],	
				shows: false, 
			}
		},
		onLoad() {
			this.loadDefaultKeyWord()
			this.loadOldKeyword()
		},
		methods: {
			loadDefaultKeyWord(){
				this.defaultKeyword = '苹果'
			},
			
			loadOldKeyword(){
				uni.getStorage({
					key:'OldKeys',
					success:res => {
						var OldKeys = JSON.parse(res.data)
						this.oldKeywordList = OldKeys
					}
				})
			},
			
		
			goPrePage(){
				uni.switchTab({
					url:'../goods/goods'
				})
			},
			
		
			clears() {
			    this.keyword=''
					this.shows = false
			},
			
		
			bindValue(e){
				this.keyword = e.detail.value
				this.shows = e.detail.value != ''?true : false
			},
			
			
			doSearch(){
				if(this.keyword == '' && this.defaultKeyword==''){ 
					uni.showToast({
						title: '请输入要搜索的内容',
						icon: 'none'
					})
					return;
				}
				if(this.keyword == ''){
					this.keyword = this.defaultKeyword
				}
				
				if(this.keyword && this.keyword !== ''){
					this.saveKeyword(this.keyword)
				}
				uni.showLoading({
					title:'搜索中'
				})
				this.onsearch()
			},
			
			
			saveKeyword(keyword){
				uni.getStorage({
					key:'OldKeys',
					success: res=>{ 
						var OldKeys = JSON.parse(res.data) 
						var findIndex = OldKeys.indexOf(keyword) 
						if(findIndex == -1){  
							OldKeys.unshift(keyword)
						}else{ 
							OldKeys.splice(findIndex,1) 
							OldKeys.unshift(keyword) 
						}
						OldKeys.length > 10 && OldKeys.pop() 
						uni.setStorage({
							key:'OldKeys',
							data:JSON.stringify(OldKeys) 
						})
						this.oldKeywordList=OldKeys 
					},
					fail:e=>{ 
						var OldKeys = [keyword];
						uni.setStorage({
						  key: 'OldKeys',
						  data: JSON.stringify(OldKeys)
						});
						this.oldKeywordList= OldKeys 
					}
				})
			},
			
			async onsearch(){
				uni.hideLoading()
				uni.navigateTo({
					url:`../goods/goodsList?keyword=${this.keyword}`
				})
			},
			
			ondoSearch(item){
				this.keyword = item
				this.onsearch()
			},
			
			oldDelete(){
				uni.showModal({
					content: '确定清除历史搜索记录？',
					success: (res) => {
						if(res.confirm){
							this.oldKeywordList=[]
							uni.removeStorage({
								key:'OldKeys',
								success() {
									uni.showToast({
										title:'已清除！',
										icon:'none'
									})
								}
							})
						} else if(res.cancel){
							console.log('用户点击取消');
						}
					}
				})
			}
		}
	}
</script>


<style scoped lang="scss">
view{display:block;}
	.search-box {background-color:rgb(242,242,242);padding:15upx 2.5%;display:flex;justify-content:space-between;position:sticky;top: 0;}
	.search-box .mSearch-input-box{width: 100%;}
	.search-box .input-box {width:85%;flex-shrink:1;display:flex;justify-content:center;align-items:center;}
	.search-box .search-btn {width:15%;margin:0 0 0 2%;display:flex;justify-content:center;align-items:center;flex-shrink:0;font-size:24upx;color:#fff;background:linear-gradient(to right,	#ff0000,#ff0000);border-radius:60upx;}
	.search-box .input-box>input {width:95%;height:60upx;font-size:24upx;-webkit-appearance:none;-moz-appearance:none;appearance:none;margin:0;background-color:#ffffff;}
	.placeholder-class {color:#9e9e9e;}
	.search-keyword {width:100%;background-color:rgb(242,242,242);}
	.keyword-list-box {height:calc(100vh - 110upx);padding-top:10upx;border-radius:20upx 20upx 0 0;background-color:#fff;}
	.keyword-entry-tap {background-color:#eee;}
	.keyword-entry {width:94%;height:80upx;margin:0 3%;font-size:30upx;color:#333;display:flex;justify-content:space-between;align-items:center;border-bottom:solid 1upx #e7e7e7;}
	.keyword-entry image {width:60upx;height:60upx;}
	.keyword-entry .keyword-text,.keyword-entry .keyword-img {height:80upx;display:flex;align-items:center;}
	.keyword-entry .keyword-text {width:90%;}
	.keyword-entry .keyword-img {width:10%;justify-content:center;}
	.keyword-box {height:calc(100vh - 110upx);border-radius:20upx 20upx 0 0;background-color:#fff;}
	.keyword-box .keyword-block {padding:10upx 0;}
	.keyword-box .keyword-block .keyword-list-header {width:94%;padding:10upx 3%;font-size:27upx;color:#333;display:flex;justify-content:space-between;}
	.keyword-box .keyword-block .keyword-list-header image {width:40upx;height:40upx;}
	.keyword-box .keyword-block .keyword {width:94%;padding:3px 3%;display:flex;flex-flow:wrap;justify-content:flex-start;}
	.keyword-box .keyword-block .hide-hot-tis {display:flex;justify-content:center;font-size:28upx;color:#6b6b6b;}
  .keyword-box .keyword-block .keyword>view {display:flex;justify-content:center;align-items:center;border-radius:38upx;padding:0 20upx;margin:10upx 20upx 10upx 0;height:60upx;font-size:24upx;background-color:rgb(242,242,242);color:#6b6b6b;line-height: 60upx;}
  .search_text{
		font-size: 24upx;
		color: #B6B6C6;
	}
	.input-box{
		position: relative;
		height: 60upx;
		font-size: 24upx;
		border: 0;
		border-radius: 60upx;
		-webkit-appearance: none;
		appearance: none;
		padding: 0 3%;
		margin: 0;
		background-color: #ffffff;
  }
  .input-box image{
    height: 40upx;
    width: 40upx;
    float: right;
    position: absolute;
    right: 2%;
    top: 50%;
    transform: translateY(-50%);
    z-index: 100;
  }
	.nohistory{
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 100%;
	}
</style>
